import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";

import { visualizer } from "rollup-plugin-visualizer";
import Compression from "vite-plugin-compression";

export default defineConfig(({ mode }) => {
  // import.meta.env.VITE_PROXY_URL
  const proxyUrl = loadEnv(mode, process.cwd()).VITE_PROXY_URL;
  // https://vitejs.dev/config/
  return {
    resolve: {
      alias: {
        "@": "/src",
      },
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue", "vue-router", "pinia", "@vueuse/core"],
        dts: "src/auto-imports.d.ts",
        eslintrc: {
          enabled: true,
        },
      }),
      Components({
        dirs: ["src/components"],
        dts: "src/components.d.ts",
      }),
      visualizer(),
      Compression(),
    ],
    build: {
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ["vue", "vue-router", "pinia", "@vueuse/core"],
            markdownIt: [
              "markdown-it",
              "markdown-it-emoji",
              "markdown-it-highlightjs",
            ],
          },
        },
      },
    },
    server: {
      proxy: {
        "/api": {
          target: proxyUrl,
          changeOrigin: true,
          // rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/captcha": {
          target: proxyUrl,
          changeOrigin: true,
        },
        "/cover": {
          target: proxyUrl,
          changeOrigin: true,
        },
        "/photo": {
          target: proxyUrl,
          changeOrigin: true,
        },
        "/avatars": {
          target: proxyUrl,
          changeOrigin: true,
        },
      },
    },
  };
});
